<template>
	<view class="view-contain">
		<public-module leftIcon="arrow-left" :title="title" />

		<!-- 进度指示器 -->
		<view class="progress-bar">
			<view class="progress-inner" :style="{ width: progressWidth + '%' }"></view>
		</view>
		<view class="progress-text">
			<text>{{ currentIndex + 1 }}/{{ totalCount }}</text>
		</view>

		<!-- 成语卡片 -->
		<view class="idiom-card" :class="{ 'card-animation': showAnimation }">
			<view class="idiom-title">{{ currentIdiom.idiom }}</view>
			<view class="idiom-pinyin">{{ currentIdiom.pinyin }}</view>
			<view class="idiom-divider"></view>
			<view class="idiom-section">
				<text class="section-label">解释：</text>
				<text class="section-content">{{ currentIdiom.explanation }}</text>
			</view>
			<view class="idiom-section">
				<text class="section-label">用法示例：</text>
				<text class="section-content">{{ currentIdiom.example }}</text>
			</view>
		</view>

		<!-- 操作按钮 -->
		<view class="action-buttons" style="position: fixed; top: 60%; transform: translateY(-50%); left: 0; right: 0; margin: 0 auto;">
			<view class="action-button prev" @click="prevIdiom" :class="{ 'disabled': currentIndex === 0 }">
				<u-icon name="arrow-left" color="#ffffff" size="20"></u-icon>
				<text>上一个</text>
			</view>
			<view class="action-button next" @click="nextIdiom"
				:class="{ 'disabled': currentIndex === totalCount - 1 }">
				<text>下一个</text>
				<u-icon name="arrow-right" color="#ffffff" size="20"></u-icon>
			</view>
		</view>

		<!-- 完成学习按钮 -->
		<view class="finish-button" v-if="currentIndex === totalCount - 1" @click="finishStudy">
			<text>完成学习</text>
		</view>
	</view>
</template>

<script>
	import {
		idiomList
	} from "@/config/api.js";

	export default {
		data() {
			return {
				title: '背成语',
				idiomList: [], // 成语列表
				currentIndex: 0, // 当前成语索引
				totalCount: 0, // 总题目数量
				statusBarHeight: 20, // 默认状态栏高度
				showAnimation: false // 控制卡片动画
			}
		},
		computed: {
			// 当前成语
			currentIdiom() {
				return this.idiomList[this.currentIndex] || {
					idiom: '',
					pinyin: '',
					explanation: '',
					example: ''
				};
			},
			// 进度条宽度
			progressWidth() {
				return (this.currentIndex + 1) / this.totalCount * 100;
			},
		},
		async onLoad(options) {
			// 获取系统信息，适配刘海屏
			uni.getSystemInfo({
				success: (res) => {
					this.statusBarHeight = res.statusBarHeight || 20;
				}
			});

			// 获取题目数量
			const count = parseInt(options.count) || 10;
			this.totalCount = count;
			
			// 调用后端接口获取成语列表
			try {
				let params = `?pageNum=1&pageSize=${count}`;
				
				const res = await idiomList(params);
				if (res && res.rows) {
					this.idiomList = this.idiomList.concat(res.rows);;
				} else {
					console.error('获取成语列表失败:', res);
					uni.showToast({
						title: '获取成语列表失败',
						icon: 'none'
					});
				}
			} catch (error) {
				console.error('调用接口出错:', error);
				uni.showToast({
					title: '网络请求失败',
					icon: 'none'
				});
			}
		},
		methods: {
			// 返回上一页
			goBack() {
				uni.navigateBack({
					delta: 1,
					fail: () => {
						uni.redirectTo({
							url: '/pages/idiom/index'
						});
					}
				});
			},

			// 上一个成语
			prevIdiom() {
				if (this.currentIndex > 0) {
					this.showAnimation = true;
					setTimeout(() => {
						this.currentIndex--;
						this.showAnimation = false;
					}, 300);
				}
			},

			// 下一个成语
			nextIdiom() {
				if (this.currentIndex < this.totalCount - 1) {
					this.showAnimation = true;
					setTimeout(() => {
						this.currentIndex++;
						this.showAnimation = false;
					}, 300);
				}
			},

			// 完成学习
			finishStudy() {
				uni.showModal({
					title: '学习完成',
					content: `恭喜您完成了${this.totalCount}个成语的学习！`,
					confirmText: '返回首页',
					cancelText: '再来一组',
					success: async (res) => {
						if (res.confirm) {
							// 返回首页
							uni.redirectTo({
								url: '/pages/idiom/index'
							});
						} else {
							// 重新开始，再次调用接口获取成语列表
							try {
								let params = `?pageNum=1&pageSize=${this.totalCount}`;
								const res = await idiomList(params);
								if (res && res.rows) {
									this.idiomList = res.rows;
									this.currentIndex = 0;
								} else {
									console.error('获取成语列表失败:', res);
									uni.showToast({
										title: '获取成语列表失败',
										icon: 'none'
									});
								}
							} catch (error) {
								console.error('调用接口出错:', error);
								uni.showToast({
									title: '网络请求失败',
									icon: 'none'
								});
							}
						}
					}
				});
			}
		}
	}
</script>

<style>
	/* 页面标题栏样式 */
	.header {
		width: 100%;
		height: 90rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #ffffff;
		padding: 0 30rpx;
		margin: 0 -30rpx 30rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}

	.back-button {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
	}

	.placeholder {
		width: 60rpx;
	}

	/* 进度条样式 */
	.progress-bar {
		width: 100%;
		height: 10rpx;
		background-color: #e0e0e0;
		border-radius: 5rpx;
		margin-bottom: 10rpx;
		overflow: hidden;
	}

	.progress-inner {
		height: 100%;
		background-color: #4A89DC;
		border-radius: 5rpx;
		transition: width 0.3s;
	}

	.progress-text {
		width: 100%;
		text-align: right;
		margin-bottom: 30rpx;
	}

	.progress-text text {
		font-size: 24rpx;
		color: #666666;
	}

	/* 成语卡片样式 */
	.idiom-card {
		width: 100%;
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 40rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
		margin-bottom: 40rpx;
		box-sizing: border-box;
		transition: all 0.3s;
	}

	.card-animation {
		opacity: 0;
		transform: translateY(20rpx);
	}

	.idiom-title {
		font-size: 48rpx;
		font-weight: bold;
		color: #333333;
		text-align: center;
		margin-bottom: 10rpx;
	}

	.idiom-pinyin {
		font-size: 28rpx;
		color: #666666;
		text-align: center;
		margin-bottom: 30rpx;
	}

	.idiom-divider {
		width: 100%;
		height: 2rpx;
		background-color: #f0f0f0;
		margin-bottom: 30rpx;
	}

	.idiom-section {
		margin-bottom: 20rpx;
	}

	.section-label {
		font-size: 28rpx;
		font-weight: bold;
		color: #333333;
		margin-bottom: 10rpx;
	}

	.section-content {
		font-size: 28rpx;
		color: #666666;
		line-height: 1.6;
	}

	/* 操作按钮样式 */
	.action-buttons {
		width: 100%;
		display: flex;
		justify-content: space-between;
	}

	.action-button {
		width: 48%;
		height: 80rpx;
		border-radius: 40rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.action-button text {
		font-size: 28rpx;
		font-weight: bold;
		color: #ffffff;
		margin: 0 10rpx;
	}

	.prev {
		background-color: #5D9CEC;
	}

	.next {
		background-color: #4A89DC;
	}

	.action-button.disabled {
		background-color: #cccccc;
		box-shadow: none;
	}

	/* 完成学习按钮 */
	.finish-button {
		width: 100%;
		height: 90rpx;
		background-color: #48CFAD;
		border-radius: 45rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		box-shadow: 0 4rpx 10rpx rgba(72, 207, 173, 0.3);
	}

	.finish-button text {
		font-size: 32rpx;
		font-weight: bold;
		color: #ffffff;
	}
</style>